﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BusStopsShowMultiplePages.aspx.cs" Inherits="GPSTrackingWebsiteManager.Pages.BusStopsShowMultiplePages" %>

<!DOCTYPE html>

<link href="../../Css/GPSTrackingWebsite.css" rel="stylesheet" />
<link href="../../Css/messi.css" rel="stylesheet" />
<script src="../../Javascript/jquery-1.11.1.min.js"></script>
<script src="../../Javascript/messi.js"></script>
<script src="../../Javascript/Custom.js"></script>
<script src="../../Javascript/WindowMessageManager.js"></script>
<script src="../../Javascript/ObjectsV2.js"></script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript">

            //Windows Message Manager 
            var objWindowsMessageManager = new WindowsMessageManager();

            var opener = GetRadWindow();

            var BusStopsManager = new BusStopsManager();
            var Companies = new Companies();

            function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow)
                    oWindow = window.radWindow;
                else if (window.frameElement.radWindow)
                    oWindow = window.frameElement.radWindow;
                return oWindow.BrowserWindow;
            }

            function initializeObjects() {
                opener.objMapControl.initializeBusStopsHistoryObject();
            }

            function getCompaniesByAdminUsername() {
                initializeObjects();
                PageMethods.wsGetCompaniesByAdminUsername(getCompaniesByAdminUsernameOk, getCompaniesByAdminUsernameError);
            }

            function getCompaniesByAdminUsernameOk(object) {
                Companies.openXMLCompaniesByAdminUsername(object, opener.globalDisplayMap.companiesList);
                Companies.loadCompaniesDataSource(getCompaniesGrid(), opener.globalDisplayMap.companiesList, opener.globalDisplayMap.companiesDataSource);
                if (opener.globalDisplayMap.companiesList.length == 1) {
                    getCompaniesGrid().selectItem(0);
                    if (BusStopsManager.isValidToGetBusRoutes(opener, opener.globalDisplayMap.companiesList[0].companyID, getBusRoutesGrid())) {
                        getBusRoutesByCompanyID();
                    }
                }
            }

            function getCompaniesByAdminUsernameError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

            function CompaniesGrid_OnRowClick(sender, args) {
                if (isValidToGetBusRoutes(args.getDataKeyValue("CompanyID"), getBusRoutesGrid())) {
                    requestStart(getrpvBusStopsManagerUpdateControl());
                    //BusStopsManager.clearSelection(getBusStopsGrid(), opener.globalBusStopsManager.busStopsList);
                    clearList(opener.globalDisplayMap.busStopsList);
                    //clearDataSource(opener.globalBusStopsManager.busStopsDataSource, getBusStopsGrid(), true);
                    getBusRoutesByCompanyID();
                }
            }

            function isValidToGetBusRoutes(companyID, tableView) {
                BusStopsManager.clearSelection(tableView, opener.globalDisplayMap.busRoutesList);
                clearList(opener.globalDisplayMap.busRoutesList);
                clearDataSource(opener.globalDisplayMap.busRoutesDataSource, tableView, true);
                index = BusStopsManager.getIndexCompanyID(opener.globalDisplayMap.companiesList, companyID);

                if (index != -1) {
                    opener.globalDisplayMap.companiesList[index].checked = true;
                    opener.globalDisplayMap.companyID = companyID;
                    opener.globalDisplayMap.busRouteID = "";
                    return true;
                }
                else {
                    return false;
                }

                return false;
            }

            function getBusRoutesByCompanyID() {
                PageMethods.wsGetBusRoutesByCompanyID(opener.globalDisplayMap.companyID, getBusRoutesByCompanyIDOk, getBusRoutesByCompanyIDError);
            }

            function getBusRoutesByCompanyIDOk(object) {
                BusStopsManager.openXMLBusRoutesByCompanyID(object, opener.globalDisplayMap.busRoutesList);
                loadBusRoutesDataSourceWait();
                responseEnd(getrpvBusStopsManagerUpdateControl());
            }

            function getBusRoutesByCompanyIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

            function loadBusRoutesDataSourceWait() {
                BusStopsManager.loadBusRoutesDataSource(getBusRoutesGrid(), opener.globalDisplayMap.busRoutesList, opener.globalDisplayMap.busRoutesDataSource);
                responseEnd(getrpvBusStopsManagerUpdateControl());
            }

            function BusRoutesGrid_OnRowClick(sender, args) {
                if (isValidToGetBusStop(args.getDataKeyValue("BusRouteID"))) {
                    requestStart(getrpvBusStopsManagerUpdateControl());
                    getBusStopsByCompanyIDAndBusRouteID();
                }
            }

            function isValidToGetBusStop(busRouteID) {

                clearList(opener.globalDisplayMap.busStopsList);

                index = BusStopsManager.getIndexBusRouteID(opener.globalDisplayMap.busRoutesList, busRouteID);

                if (index != -1) {
                    opener.globalDisplayMap.busRoutesList[index].checked = true;
                    opener.globalDisplayMap.busRouteID = busRouteID;
                    return true;
                }
                else {
                    return false;
                }
            }
            
            function getBusStopsByCompanyIDAndBusRouteID() {
                PageMethods.wsGetBusStopsByCompanyIDAndBusRouteID(opener.globalDisplayMap.companyID, opener.globalDisplayMap.busRouteID, getBusStopsByCompanyIDAndBusRouteIDOk, getBusStopsByCompanyIDAndBusRouteIDError);
            }

            function getBusStopsByCompanyIDAndBusRouteIDOk(object) {
                BusStopsManager.openXMLBusStopsByCompanyAndBusRouteID(opener.objMapControl, object, opener.globalDisplayMap.busStopsList);
                responseEnd(getrpvBusStopsManagerUpdateControl());
                //BusStopsManager.loadBusStopDataSource(getBusStopsGrid(), opener.globalBusStopsManager.busStopsList, opener.globalBusStopsManager.busStopsDataSource);
                //GetAllBusRouteDetailsByBusRouteID();
                //for (i = 0; i < opener.globalDisplayMap.busStopsList.length; i++) {
                    //getBusStopsGrid().selectItem(i);
                    
                    //opener.globalDisplayMap.busStopsList[i].checked = true;
                    //if (getCkbCenterBusStops().checked) {
                    //    opener.objMapControl.centerMap(opener.objMapControl.getPositionMarker(opener.globalBusStopsManager.busStopsList[i].marker));
                    //}

                    //if (getCkbZoomBusStops().checked) {
                    //    opener.objMapControl.zoomMap(16);
                    //}
                    //BusStopsManager.showAllBusStopsChecked(opener.globalBusStopsManager.busStopsList);
                //}
            }

            function getBusStopsByCompanyIDAndBusRouteIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

            function GetAllBusRouteDetailsByBusRouteID() {
                PageMethods.wsGetAllBusRouteDetailsByBusRouteID(opener.globalBusStopsManager.busRouteID, GetAllBusRouteDetailsByBusRouteIDOk, GetAllBusRouteDetailsByBusRouteIDError);
            }

            function GetAllBusRouteDetailsByBusRouteIDOk(object) {
                BusStopsManager.clearBusRoutes(opener);
                BusStopsManager.openXMLBusRouteDetailsByBusRouteID(object, opener.globalBusStopsManager);

                opener.globalBusStopsManager.temporalyIndex = 0;
                opener.globalBusStopsManager.temporalyInterval = setInterval(function () { loadBusRouteDisplay(opener); }, opener.globalBusStopsManager.timeToCalculateRoute);
            }

            function loadBusRouteDisplay(opener) {
                if (opener.globalBusStopsManager.temporalyIndex < opener.globalBusStopsManager.temporalyList.length) {
                    opener.objMapControl.addBusRouteClickDB(opener.globalBusStopsManager.temporalyList[opener.globalBusStopsManager.temporalyIndex++]);
                }
                else {
                    clearInterval(opener.globalBusStopsManager.temporalyInterval);
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                }
            }

            function GetAllBusRouteDetailsByBusRouteIDError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

            function BusStopsGrid_RowSelecting(sender, args) {
                BusStopsManager.busStopGridRowSelecting(opener, args, getCkbZoomBusStops().checked, getCkbCenterBusStops().checked);
            }

            function BusStopsGrid_OnRowClick(sender, args) {
                if (BusStopsManager.busStopGridOnRowClick(opener, args)) {
                    BusStopDeleteCompletely();
                }
            }

            function BusStopDeleteCompletely() {
                requestStart(getrpvBusStopsManagerUpdateControl());
                //getBusStopsGrid().clearSelectedItems();

                if (opener.globalBusStopsManager.busStopID != -1) {
                    PageMethods.wsBusStopDelete(opener.globalBusStopsManager.busRouteID, opener.globalBusStopsManager.busStopID, BusStopDeleteCompletelyOk, BusStopDeleteCompletelyError);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    objWindowsMessageManager.show_Warning("Debe seleccionar una parada de bus para eliminarlo.", titles.busStopsTitle);
                }
            }

            function BusStopDeleteCompletelyOk(object) {
                if (object = "true") {
                    //BusStopsManager.refreshAfterDeleteBusStop(opener, getBusStopsGrid());
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    objWindowsMessageManager.show_Info("Se eliminó correctamente la parada de bus.", titles.busStopsTitle);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    objWindowsMessageManager.show_Error("No se eliminó correctamente la parada de bus.", titles.busStopsTitle);
                }
            }

            function BusStopDeleteCompletelyError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

            function BusStopsGrid_RowDeselecting(sender, args) {
                var index = BusStopsManager.getIndexBusStopID(opener.globalBusStopsManager.busStopsList, args.getDataKeyValue("BusStopID"));
                if (index != -1) {
                    opener.globalBusStopsManager.busStopsList[index].checked = false;
                    BusStopsManager.showAllBusStopsChecked(opener.globalBusStopsManager.busStopsList);
                }
            }

            function getBusRoutesGrid() {
                try {
                    return $find("<%= rgBusRoutesGrid.ClientID %>").get_masterTableView();
                }
                catch (ex) {
                    setTimeout(getBusRoutesGrid, 100);
                }
            }

            function getCompaniesGrid() {
                try {
                    return $find("<%= rgCompaniesGrid.ClientID %>").get_masterTableView();
                }
                catch (ex) {
                    setTimeout(getCompaniesGrid, 100);
                }
            }

            

            function getrpvBusStopsManagerUpdateControl() {
                return "<%=  rpvBusStopsManager.ClientID %>";
            }

            function enableDisableBusStopHalfTerminalEventClickMap() {
               
                //if (getCkbHalfTerminalStops().checked) {
                    getCkbTerminalStops().checked = false;
                    getCkbBusStops().checked = false;
                //}

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbHalfTerminalStops().checked);
            }

            function enableDisableBusStopTerminalEventClickMap() {
                
                if (getCkbTerminalStops().checked) {
                    getCkbBusStops().checked = false;
                    getCkbHalfTerminalStops().checked = false;
                }

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbTerminalStops().checked);
            }

            function enableDisableBusStopEventClickMap() {

                if (getCkbBusStops().checked)
                {
                    getCkbTerminalStops().checked = false;
                    getCkbHalfTerminalStops().checked = false;
                }

                BusStopsManager.enableDisableBusStopEventClickMap(opener, getCkbBusStops().checked);
            }

            function requestStart(object) {
                try {
                    $find("<%= RadAjaxLoadingPanel1.ClientID%>").show(object);
                }
                catch (e) {
                    null;
                }
            }

            function responseEnd(object) {
                try {
                    $find("<%= RadAjaxLoadingPanel1.ClientID%>").hide(object);
                }
                catch (e) {
                    null;
                }
            }

            function DisplayMap() {
                
                var wnd = opener.radopen("ShowRoutes.aspx");
                wnd.setSize(800, 450);
                wnd.set_title("Visualizador de Rutas");
                wnd.set_visibleStatusbar(false);
                wnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize + Telerik.Web.UI.WindowBehaviors.Minimize + Telerik.Web.UI.WindowBehaviors.Move);
            }

            function BusStopInsert(objects) {
                PageMethods.wsBusStopInsert(opener.globalBusStopsManager.companyID, opener.globalBusStopsManager.busRouteID, objects.busStopName, objects.lat, objects.lng, getCkbTerminalStops().checked, getCkbHalfTerminalStops().checked, BusStopInsertOk, BusStopInsertError);
            }

            function BusStopInsertOk(id) {
                if (id != -1) {
                    //BusStopsManager.busStopInsertSuccessful(opener, id, getBusStopsGrid(), getRtbBusStopsName().value);
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    //getRtbBusStopsName().value = "";
                    objWindowsMessageManager.show_Info("Se registró correctamente la parada de Bus.", titles.busStopsTitle);
                }
                else {
                    responseEnd(getrpvBusStopsManagerUpdateControl());
                    objWindowsMessageManager.show_Warning("El nombre de la parada de Bus ya está registrado para esta compañía, cambiar el nombre de la parada de Bus.", titles.busStopsTitle);
                }
            }

            function BusStopInsertError(error) {
                responseEnd(getrpvBusStopsManagerUpdateControl());
                objWindowsMessageManager.show_Error(error.get_message(), titles.busStopsTitle);
            }

        </script>
    </telerik:RadScriptBlock>
    <style type="text/css">
        .auto-style1
        {
            width: 153px;
        }
    </style>
</head>
<body onload="getCompaniesByAdminUsername()">

    <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true"></telerik:RadScriptManager>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
					<telerik:AjaxSetting AjaxControlID="AjaxControlID1">
						<UpdatedControls>
							<telerik:AjaxUpdatedControl ControlID="Loading" LoadingPanelID="RadAjaxLoadingPanel1" />
						</UpdatedControls>
					</telerik:AjaxSetting>
				</AjaxSettings>
        </telerik:RadAjaxManager>

        <telerik:RadTabStrip ID="rtsBusStopsManager" runat="server" SelectedIndex="0" MultiPageID="rmpBusStopsManager" >
        </telerik:RadTabStrip>
        <telerik:RadMultiPage ID="rmpBusStopsManager" runat="server"  SelectedIndex="0">
            <telerik:RadPageView ID="rpvBusStopsManager" runat="server">

                <table style="width: 100%; text-align: left">
                    <tr>
                        <td>
                  
				            <fieldset>
					            <legend>Compañias Registradas</legend>
					            <telerik:RadGrid ID="rgCompaniesGrid" runat="server" AllowMultiRowSelection="False" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Height="100px" OnNeedDataSource="rgCompaniesGrid_NeedDataSource"  Width="250px">
						            <ClientSettings>
							            <Selecting AllowRowSelect="True" />
							            <ClientEvents OnRowClick="CompaniesGrid_OnRowClick" />
							            <Scrolling AllowScroll="True" UseStaticHeaders="true" />
						            </ClientSettings>
						            <MasterTableView EditMode="PopUp" EditFormSettings-PopUpSettings-Modal="true" EditFormSettings-CaptionFormatString="Modo Edición" ClientDataKeyNames="CompanyID" NoMasterRecordsText="No hay compañias registradas">
							            <CommandItemSettings ExportToPdfText="Export to PDF" />
							            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
							            </RowIndicatorColumn>
							            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
							            </ExpandCollapseColumn>
							            <Columns>
								            <telerik:GridBoundColumn DataField="CompanyID" FilterControlAltText="Filter Description column" HeaderText="ID" UniqueName="CompanyID" Display="false">
								            </telerik:GridBoundColumn>
								            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Description column" HeaderText="Nombre" Visible="true">
								            </telerik:GridBoundColumn>
							            </Columns>
							            <EditFormSettings>
								            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
								            </EditColumn>
							            </EditFormSettings>
						            </MasterTableView>
						            <FilterMenu EnableImageSprites="true">
						            </FilterMenu>
					            </telerik:RadGrid>
				            </fieldset>
			            </td>
			            <td>
                  
				            <fieldset>
					            <legend>Grupos de Rutas Registrados</legend>
					            <telerik:RadGrid ID="rgBusRoutesGrid" runat="server" AllowMultiRowSelection="False" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Height="100px" OnNeedDataSource="rgBusRoutesGrid_NeedDataSource"  Width="250px">
						            <ClientSettings>
							            <Selecting AllowRowSelect="True" />
							            <ClientEvents OnRowClick="BusRoutesGrid_OnRowClick" />
							            <Scrolling AllowScroll="True" UseStaticHeaders="true" />
						            </ClientSettings>
						            <MasterTableView EditMode="PopUp" EditFormSettings-PopUpSettings-Modal="true" EditFormSettings-CaptionFormatString="Modo Edición" ClientDataKeyNames="BusRouteID" NoMasterRecordsText="No hay Grupos de Rutas registrados">
							            <CommandItemSettings ExportToPdfText="Export to PDF" />
							            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
							            </RowIndicatorColumn>
							            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
							            </ExpandCollapseColumn>
							            <Columns>
								            <telerik:GridBoundColumn DataField="BusRouteID" FilterControlAltText="Filter Description column" HeaderText="ID" UniqueName="BusRouteID" Display="false">
								            </telerik:GridBoundColumn>
								            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Description column" HeaderText="Nombre" Visible="true">
								            </telerik:GridBoundColumn>
							            </Columns>
							            <EditFormSettings>
								            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
								            </EditColumn>
							            </EditFormSettings>
						            </MasterTableView>
						            <FilterMenu EnableImageSprites="true">
						            </FilterMenu>
					            </telerik:RadGrid>
				            </fieldset>
			            </td>
			           <td>
                            <input id="btnBusStop" type="button" value="Visualizar" onclick="DisplayMap();" />
                       </td>
		            </tr>
	            </table>
            </telerik:RadPageView>
        </telerik:RadMultiPage>
         

                <telerik:RadWindowManager ID="RadWindowManager" runat="server" Title="Cuenta" ShowContentDuringLoad="false" Width="330px" VisibleStatusbar="false"
									   Height="550px" Behaviors="Move,Close,Resize, Minimize">
			</telerik:RadWindowManager>   

        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default"></telerik:RadAjaxLoadingPanel>
    </form>
</body>
</html>
